import type { DefaultWalletOptions, Wallet } from '../../../types/Wallet'
import { getInjectedConnector, hasInjectedProvider } from '../../getInjectedConnector'
import { getWalletConnectConnector } from '../../getWalletConnectConnector'
import {bybitWalletIcon} from './bybitWalletIcon'

export type BifrostWalletOptions = DefaultWalletOptions

export const bybitWallet = ({ projectId, walletConnectParameters }: BifrostWalletOptions): Wallet => {
  const isBybitInjected = hasInjectedProvider({
    namespace: 'bybitWallet'
  })

  const shouldUseWalletConnect = !isBybitInjected

  const getUri = (uri: string) => {
    return `bybitapp://open/route?targetUrl=by://web3/walletconnect/wc?uri=${encodeURIComponent(uri)}`
  }

  return {
    id: 'bybit',
    name: 'Bybit Wallet',
    rdns: 'com.bybit',
    iconUrl: bybitWalletIcon,
    installed: !shouldUseWalletConnect ? isBybitInjected : undefined,
    iconBackground: '#000000',
    downloadUrls: {
      chrome: 'https://chromewebstore.google.com/detail/bybit-wallet/pdliaogehgdbhbnmkklieghmmjkpigpa',
      browserExtension: 'https://www.bybit.com/en/web3',
      android: 'https://play.google.com/store/apps/details?id=com.bybit.app',
      ios: 'https://apps.apple.com/us/app/bybit-buy-trade-crypto/id1488296980',
      mobile: 'https://www.bybit.com/en/web3',
      qrCode: 'https://www.bybit.com/en/web3'
    },
    mobile: {
      getUri: shouldUseWalletConnect ? getUri : undefined
    },
    qrCode: shouldUseWalletConnect
      ? {
          getUri: (uri: string) => uri,
          instructions: {
            learnMoreUrl: 'https://www.bybit.com/en/web3',
            steps: [
              {
                description: 'We recommend putting Bybit on your home screen for faster access to your wallet.',
                step: 'install',
                title: 'Open the Bybit app'
              },
              {
                description: 'You can easily backup your wallet using our backup feature on your phone.',
                step: 'create',
                title: 'Create or Import a Wallet'
              },
              {
                description: 'After you scan, a connection prompt will appear for you to connect your wallet.',
                step: 'scan',
                title: 'Tap the scan button'
              }
            ]
          }
        }
      : undefined,
    extension: {
      instructions: {
        learnMoreUrl: 'https://www.bybit.com/en/web3',
        steps: [
          {
            description: 'Click at the top right of your browser and pin Bybit Wallet for easy access.',
            step: 'install',
            title: 'Install the Bybit Wallet extension'
          },
          {
            description: 'Create a new wallet or import an existing one.',
            step: 'create',
            title: 'Create or Import a wallet'
          },
          {
            description: 'Once you set up Bybit Wallet, click below to refresh the browser and load up the extension.',
            step: 'refresh',
            title: 'Refresh your browser'
          }
        ]
      }
    },
    createConnector: shouldUseWalletConnect
      ? getWalletConnectConnector({
          projectId,
          walletConnectParameters
        })
      : getInjectedConnector({
          namespace: 'bybitWallet'
        })
  }
}
